package com.example.articletest.controller;

import com.example.articletest.POJO.Comment;
import com.example.articletest.Service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class CommenController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/save")
    public String save() {
        Comment comment=new Comment();
        comment.setArticleid("100000");
        comment.setContent("测试添加的数据");
        comment.setCreatedatetime(LocalDateTime.now());
        comment.setUserid("1003");
        comment.setNickname("凯撒大帝");
        comment.setState("1");
        comment.setLikenum(0);
        comment.setReplynum(0);

        commentService.saveComment(comment);

        return "success";
    }

    //测试查询所有--返回给前端json--即返回POJO对象即可
    @GetMapping("/findAll")
    public List<Comment> testFindAll(){
        List<Comment> list = commentService.findCommentList();
        System.out.println(list);
        return list;
    }

    /**
     * 测试根据id查询--这里id是MongoDB自动生成的主键，而不是我们自定义的
     */
    @GetMapping("/findById")
    public void testFindCommentById(){
        Comment comment = commentService.findCommentById("5d6a27b81b8d374798cf0b41");
        System.out.println(comment);
    }

    /**
     * 测试分页查询Page类型来接受
     */
    @GetMapping("/page")
    public List<Comment> testPage(){
        Page<Comment> page = commentService.page();
        //同PageHelper一样，Page对象也有很多方法
        // 获取分页查询结果数据
        List<Comment> comments = page.getContent();

// 获取分页信息--总页数、总记录数、当前页、每页显示的记录数等
        int pageNumber = page.getNumber();
        int pageSize = page.getSize();
        int totalPages = page.getTotalPages();
        long totalElements = page.getTotalElements();

        return comments;
    }
}
